package com.newworld.dmsapp2.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.newworld.dmsapp2.BaseActivity;
import com.newworld.dmsapp2.R;
import com.newworld.dmsapp2.activity.ToDoTaskListActivity.getTasks;
import com.newworld.dmsapp2.activity.history.HistorySearchTimeLineActivity;
import com.newworld.dmsapp2.bean.Task;
import com.newworld.dmsapp2.bean.User;
import com.newworld.dmsapp2.fragment.adapter.HistorySearchAdapter;
import com.newworld.dmsapp2.fragment.adapter.TodoTaskListAdapter;
import com.newworld.dmsapp2.fragment.bean.History;
import com.newworld.dmsapp2.ui.view.SearchClearEditText;
import com.newworld.dmsapp2.ui.view.XListView;
import com.newworld.dmsapp2.ui.view.XListView.IXListViewListener;
import com.newworld.dmsapp2.util.Config;
import com.newworld.dmsapp2.util.HttpUtil;
import com.newworld.dmsapp2.util.SingleSample;
import com.newworld.dmsapp2.util.Tool;

@SuppressLint({ "NewApi", "CutPasteId" })
public class HistorySearchActivity extends BaseActivity implements
		IXListViewListener {

	private XListView historyLv;
	private HistorySearchAdapter historySearchAdapter;
	private SearchClearEditText mClearEditText;
	private LinearLayout title_list_choose_centerly;
	private LinearLayout title_centeterly;
	private LinearLayout classify_l2;// 隐藏的项目列表
	private TextView titletext;
	private ListView titleListView1;
	private TitleListAdapter titleAdapter;
	private TextView searchbtn;
	private ProgressBar loginbar;
	List<Task> gettasks = new ArrayList<Task>();
	public static ArrayList<History> historyList = new ArrayList<History>();
	private List<History> refreshTasks = new ArrayList<History>();
	private User user;
	public static String HISTORY_STATUS;
	private String realfunctioncode;
	private String houseadress;
	protected String currentPage = "1";
	protected int page = 1;
	private String areaType = Config.AREACODE_ALL;
	private String functiontitlename;
	private String sessionId;
	private boolean refresh = false;
	//判断是否是搜索获取到的列表
	private boolean issearch = false;
	public static boolean HISTORY_REFRESH_FLAG = true,moremessageflag = true;

	@SuppressWarnings("unused")
	private String editstring;
	Handler mHandler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {

			case Config.HANDLER_MSG_GETDATA_SUCCESS:
				/*if (null == refresh || refreshTasks.size() == 0) {
					mainview.hideFoot1();
				} else {
					mainview.showFoot();
				}*/
				//refreshTasks = (List<Task>) msg.obj;
				
				refreshTasks = (List<History>) msg.obj;
				System.out.println("refreshTasks.size是："+refreshTasks.size());
				System.out.println("page是："+page);
				 if (refreshTasks.size() == 0) {
					 if (refresh) {
						 historyList.clear();
					 }
					 historyLv.hideFoot1();
				 	} else {
				 		if (refresh) {
				 			historyList.clear();
				 			historyList.addAll((List<History>) msg.obj);
				 			historyLv.showFoot();
				 		} else {
				 			historyList.addAll((List<History>) msg.obj);
				 			historyLv.showFoot();
				 		}
				 }

				if (null == refreshTasks || refreshTasks.size() == 0) {
					historyLv.hideFoot1();
				} else {
					historyLv.showFoot();
				}
				/*if (!moremessageflag) {
					historyLv.hideFoot1();
				} else {
					historyLv.showFoot();
				}*/
				historySearchAdapter = new HistorySearchAdapter(
						HistorySearchActivity.this, historyList);
				historyLv.setAdapter(historySearchAdapter);
				// showShortToast((String) msg.obj);
				if(page != 1){
					historyLv.setSelection(historyLv.getCount() - 1); 
				}
				showShortToast("加载成功");
				loginbar.setVisibility(View.INVISIBLE);
				historySearchAdapter.notifyDataSetChanged();
				onLoad();
				break;
			case Config.HANDLE_MSG_SEARCH_FAIL:
				Toast.makeText(HistorySearchActivity.this, (String) msg.obj,
						Toast.LENGTH_SHORT).show();
				loginbar.setVisibility(View.INVISIBLE);
				onLoad();
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_history_search_list);
		initData();
		initViews();
		initEvents();
		loginbar.setVisibility(View.VISIBLE);

	}


	@Override
	protected void initViews() {
		// TODO Auto-generated method stub
	
		
		LinearLayout leftlayout = (LinearLayout) findViewById(R.id.titlebar_list_choose_left_layout);
		leftlayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
		loginbar = (ProgressBar) findViewById(R.id.history_list_progressBar);
		historyLv = (XListView) findViewById(R.id.history_lv);
		title_centeterly = (LinearLayout) findViewById(R.id.title_list_choose_centerly);
		title_list_choose_centerly = (LinearLayout) findViewById(R.id.title_list_choose_list);
		titletext = (TextView) findViewById(R.id.titlebar_list_choose_center_title);
		titleListView1 = (ListView) findViewById(R.id.titleListView1);

		title_list_choose_centerly.setVisibility(View.INVISIBLE);
		title_centeterly.setVisibility(View.VISIBLE);

		classify_l2 = (LinearLayout) findViewById(R.id.classify_l2);
		titletext.setText(functiontitlename);
		Config.TITLE_ARRAY[0] = functiontitlename;
		titleListView1.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// 切换标题
				if (position == 0) {
					Config.TITLE_ARRAY[position] = functiontitlename;
					titletext.setText(Config.TITLE_ARRAY[position]);
				} else {
					titletext.setText(Config.TITLE_ARRAY[position]);
				}
				areaType = Config.AREACODE_ARRAY[position];
				Log.e(areaType, "areaType-->" + areaType + "" + position
						+ "position");

				if (classify_l2.isShown()) {
					classify_l2.setVisibility(View.GONE);
				}
				onRefresh();
			}

		});
		classify_l2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				classify_l2.setAnimation(AnimationUtils.loadAnimation(
						HistorySearchActivity.this, R.anim.push_top_out2));

				classify_l2.setVisibility(View.GONE);
			}
		});
		title_centeterly.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (classify_l2.isShown()) {

					classify_l2.setAnimation(AnimationUtils.loadAnimation(
							HistorySearchActivity.this, R.anim.push_top_out2));

					classify_l2.setVisibility(View.GONE);

				} else {
					List<Map<String, Object>> titlebarlists = new ArrayList<Map<String, Object>>();
					for (int i = 0; i < Config.TITLE_ARRAY.length; i++) {
						Map<String, Object> titlebarlist = new HashMap<String, Object>();
						titlebarlist.put("title", Config.TITLE_ARRAY[i]);
						titlebarlists.add(titlebarlist);
					}
					classify_l2.setAnimation(AnimationUtils.loadAnimation(
							HistorySearchActivity.this, R.anim.push_top_in2));

					classify_l2.setVisibility(View.VISIBLE);
					titleAdapter = new TitleListAdapter(
							HistorySearchActivity.this, titlebarlists,
							R.layout.nw_task_title_list_item,
							new String[] { "title" },
							new int[] { R.id.task_title_tv });
					titleListView1.setAdapter(titleAdapter);
				}
				System.out.println("areaType----first---11" + areaType);
				//onRefresh();
			}
		});

		historyLv.setPullLoadEnable(true);
		historyLv.setPullRefreshEnable(true);// 下拉刷新
		historyLv.setXListViewListener(this);// 给xListView设置监听 ******
		preferences = PreferenceManager
				.getDefaultSharedPreferences(HistorySearchActivity.this);
		// 搜索
		mClearEditText = (SearchClearEditText) findViewById(R.id.search_bar_text);
		searchbtn = (TextView) findViewById(R.id.search_bar_finish);
		mClearEditText.setFocusable(true);
		InputMethodManager imm = (InputMethodManager) mClearEditText
				.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(mClearEditText.getWindowToken(), 0);
		// 根据输入框输入值的改变来过滤搜索
		mClearEditText.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// 当输入框里面的值为空，更新为原来的列表，否则为过滤数据列表
				/*
				 * editstringbuffer.append(s);
				 * editstring=editstringbuffer.toString();
				 */
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				editstring = s.toString();
				/*
				 * String search = editstring;
				 * if(editstring.equals("")||editstring.equals("请输入关键词")){
				 * search = ""; }else{ search = editstring; } page = 0;
				 * HashMap<String, String> orderList = new HashMap<String,
				 * String>(); orderList.put("statusType_typeCode", "ASC");
				 * //orderList.put("statusType_typeCode", "DESC");
				 * orderList.put("reporttime", "ASC");
				 * //orderList.put("reporttime", "DESC"); currentPage =
				 * String.valueOf(page); new
				 * getSearchTasks(currentPage,orderList,search).start();
				 */

			}
		});
		searchbtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				  issearch = true;
				  historyList.clear();
				  String search = editstring;
				  if (editstring.equals("") ||
						  editstring.equals("请输入关键词")) { search = ""; 
				  } else { 
					  search = editstring; 
				  } 
				  page = 1;
				  HashMap<String, String> orderList =new HashMap<String, String>();
				  orderList.put("statusType_typeCode", "ASC"); //
				  /*orderList.put("statusType_typeCode", "DESC");
				  orderList.put("reporttime", "ASC"); //
				  orderList.put("reporttime", "DESC");*/ 
				  currentPage = String.valueOf(page); 
				  new getHistoryList(currentPage, orderList, search).start();
				 
			}
		});

	}

	private void initData() {
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		functiontitlename = bundle.getString(Config.FUNCTIONTITLENAME);
		user = SingleSample.getInstance(this).getUser();
		if (functiontitlename.equals(Config.FUNCTIONTITLENAME_HISTORY)) {
			if (null != user
					.getFunctionlistFromUser(Config.FUNCTION_REAL_HISTORYLIST_OWNER)
					&& user.getFunctionlistFromUser(
							Config.FUNCTION_REAL_HISTORYLIST_OWNER).get(0) != null) {// 户主
				realfunctioncode = Config.FUNCTION_REAL_HISTORYLIST_OWNER;
				
			} else if (null != user
					.getFunctionlistFromUser(Config.FUNCTION_REAL_HISTORYLIST_NONOWENER)
					&& user.getFunctionlistFromUser(
							Config.FUNCTION_REAL_HISTORYLIST_NONOWENER).get(0) != null) {// 非户主
				realfunctioncode = Config.FUNCTION_REAL_HISTORYLIST_NONOWENER;
				
			} else if (null != user
					.getFunctionlistFromUser(Config.FUNCTION_REAL_HISTORYLIST_DISPATCHER)
					&& user.getFunctionlistFromUser(
							Config.FUNCTION_REAL_HISTORYLIST_DISPATCHER).get(0) != null) {// 派单员
				realfunctioncode = Config.FUNCTION_REAL_HISTORYLIST_DISPATCHER;
			} else if (null != user
					.getFunctionlistFromUser(Config.FUNCTION_REAL_HISTORYLIST_SERVER)
					&& user.getFunctionlistFromUser(
							Config.FUNCTION_REAL_HISTORYLIST_SERVER).get(0) != null) {// 服务人员
				realfunctioncode = Config.FUNCTION_REAL_HISTORYLIST_SERVER;
			} else if (null != user
					.getFunctionlistFromUser(Config.FUNCTION_REAL_HISTORYLIST_PM)
					&& user.getFunctionlistFromUser(
							Config.FUNCTION_REAL_HISTORYLIST_PM).get(0) != null) {// 项目经理
				realfunctioncode = Config.FUNCTION_REAL_HISTORYLIST_PM;
			} else if (null != user
					.getFunctionlistFromUser(Config.FUNCTION_REAL_HISTORYLIST_SERVERMANAGER)
					&& user.getFunctionlistFromUser(
							Config.FUNCTION_REAL_HISTORYLIST_SERVERMANAGER)
							.get(0) != null) {// 部门经理
				realfunctioncode = Config.FUNCTION_REAL_HISTORYLIST_SERVERMANAGER;
			} else if (null != user
					.getFunctionlistFromUser(Config.FUNCTION_REAL_HISTORYLIST_SERVERMANAGER)
					&& user.getFunctionlistFromUser(
							Config.FUNCTION_REAL_HISTORYLIST_DEPARTDISPATCHER)
							.get(0) != null) {// 部门派单员
				realfunctioncode = Config.FUNCTION_REAL_HISTORYLIST_DEPARTDISPATCHER;
			}
		} else if (functiontitlename
				.equals(Config.FUNCTIONTITLENAME_REPORTE_HISTORY)) {
			if (null != user
					.getFunctionlistFromUser(Config.FUNCTION_REAL_HISTORYLIST_WORKER)
					&& user.getFunctionlistFromUser(
							Config.FUNCTION_REAL_HISTORYLIST_WORKER).get(0) != null) {// 部门派单员
				realfunctioncode = Config.FUNCTION_REAL_HISTORYLIST_WORKER;
			}
		}
		
		

		if (null != user) {
			if (null != user.getSessionId()
					&& !user.getSessionId().equals("")) {
				sessionId = user.getSessionId();
			}
		}
	}


	@Override
	protected void initEvents() {
		// TODO Auto-generated method stub
		historyLv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub
				position = position - 1;
				if (!(position < 0)) {
					HISTORY_STATUS = historyList.get(position).getStatus();
					Bundle b = new Bundle();
					b.putString(Config.REPAIRID, historyList.get(position)
							.getRepairId());
					b.putString(Config.HOUSE_ADRESS, houseadress);
					b.putString(Config.AREATYPETYPECODE, historyList.get(position).getAreaType_typeCode());
					b.putString("functiontitlename", functiontitlename);
					Log.e(historyList.get(position).getRepairId(),
							"historyList.get(position).getRepairId()--11"
									+ historyList.get(position).getRepairId());
					Log.e(houseadress, "houseadress--11" + houseadress);
					Log.e(historyList.get(position).getAreaType_typeCode(), "historyList.get(position).getAreaType_typeCode()--11" + historyList.get(position).getAreaType_typeCode());
					Intent i = new Intent(HistorySearchActivity.this,
							HistorySearchTimeLineActivity.class);
					i.putExtras(b);
					startActivity(i);
				}
			}

		});
	}

	/**
	 * 重写Adapter
	 * 
	 * @author Administrator
	 * 
	 */
	class TitleListAdapter extends SimpleAdapter {
		// private int[] colors = new int[] { 0x30FF0000, 0x300000FF };

		public TitleListAdapter(Context context,
				List<? extends Map<String, ?>> data, int resource,
				String[] from, int[] to) {
			super(context, data, resource, from, to);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			Log.e("TitleListAdapter", "getCount()");
			return super.getCount();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Log.e("TitleListAdapter", "getView()");
			View view = super.getView(position, convertView, parent);

			//TextView item = (TextView) view.findViewById(R.id.title_list_tv);
		
			return view;
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) { // 监控/拦截/屏蔽返回键
			onBackPressed();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		if (Tool.isNetworkAvailable(HistorySearchActivity.this)) {
			historyList.clear();
			refresh = true;
			String search = "";
			page = 1;
			HashMap<String, String> orderList = new HashMap<String, String>();
			orderList.put("statusType_typeCode", "ASC");
			orderList.put("reporttime", "ASC");
			currentPage = String.valueOf(page);
			new getHistoryList(currentPage, orderList, search).start();
		}else {

			String message = "请设置网络";
			mHandler.obtainMessage(Config.HANDLE_MSG_SEARCH_FAIL,
					message).sendToTarget();

			/* start = ++refreshCnt; */
		}
		
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		if (Tool.isNetworkAvailable(HistorySearchActivity.this)) {
		refresh = false;
		page++;
		String search = mClearEditText.getText() + "";
		HashMap<String, String> orderList = new HashMap<String, String>();
		orderList.put("statusType_typeCode", "ASC");
		orderList.put("reporttime", "ASC");
		currentPage = String.valueOf(page);
		new getHistoryList(currentPage, orderList, search).start();
		} else {

			String message = "请设置网络";
			mHandler.obtainMessage(Config.HANDLE_MSG_SEARCH_FAIL,
					message).sendToTarget();

			/* start = ++refreshCnt; */
		}

	}

	@Override
	public void onResume() {
		super.onResume();
		if(HISTORY_REFRESH_FLAG){
			onRefresh();
		}
		

	}

	private void onLoad() {
		historyLv.stopRefresh();
		historyLv.stopLoadMore();
		historyLv.setRefreshTime(refreshUpdatedAtValue());
	}

	class getHistoryList extends Thread {
		public HashMap<String, String> orderList;
		public String curPage;
		public String search = "";

		public getHistoryList(String curPage,
				HashMap<String, String> orderList, String search) {
			super();
			this.orderList = orderList;
			this.curPage = curPage;
			this.search = search;
		}

		@Override
		public void run() {

			if (Tool.isNetworkAvailable(HistorySearchActivity.this)) {
				try {
					String result = null;
					String url = Config.SERVER_URL
							+ user.getFunctionlistFromUser(realfunctioncode)
									.get(0).getFunctionUrl();
					//List<Task> gettasks = new ArrayList<Task>();
					Map<String, String> map = new HashMap<String, String>();
					System.out.println("url----22" + url);
					map.put("page", curPage);
					map.put("search", search);
					//map.put("orderList", new Gson().toJson(orderList));
					map.put("functionCode", realfunctioncode);
					System.out.println("realfunctioncode----22" + realfunctioncode);
					map.put("areaType_typecode", areaType);
					System.out.println("areaType----22" + areaType);
					map.put("repairId", "");
				
					result = HttpUtil.postRequestSe(url, map, sessionId);
					Log.e("页数", curPage);
					Log.e("搜索的result", result);
					String total = new JSONObject(result).getString("total");
					String message = new JSONObject(result)
							.getString("message");
					String flag = new JSONObject(result).getString("flag");
					String rows = new JSONObject(result).getString("rows");
					java.lang.reflect.Type type = new TypeToken<List<Task>>() {
					}.getType();
					Gson gson = new Gson();
					gettasks = gson.fromJson(rows, type);
					if (flag.equals(Config.CONNECT_SUCCESS2)) {
						ArrayList<History> searchhs = new ArrayList<History>();
						
						//historyList.clear();
						if (!total.equals("0")) {
							for (Task t : gettasks) {
								boolean exitflag = false;
								for (History hc : historyList) {
									if (hc.getRepairId()
											.equals(t.getRepairId())) {
										exitflag = true;
									}
								}
								if (!exitflag) {
									History h = new History();
									h.setRepairId(t.getRepairId());
									if (t.getAreaType_typeCode().equals(
											Config.AREACODE_OUTDOOR)) {
										houseadress = t.getProjectName()
												+ t.getAreaDetail();
									} else {
										houseadress = t.getProjectName()
												+ t.getPhaseName()
												+ t.getBuildingName()
												+ t.getUnitName()
												+ t.getHouseName();
									}
									h.setAreaType_typeCode(t.getAreaType_typeCode());
									h.setLocation(houseadress);
									h.setRepairPeople(t.getServer_realName());
									if (t.getFinishTime().length() > Config.TIMELENGTH) {
										h.setTime(t.getFinishTime().substring(
												0, Config.TIMELENGTH));
									} else {
										h.setTime(t.getFinishTime());
									}
									h.setStatus("完");
									searchhs.add(h);
									//historyList.add(h);
								}

							}
							mHandler.obtainMessage(
									Config.HANDLER_MSG_GETDATA_SUCCESS,
									searchhs).sendToTarget();
						} else{
							//historyList.clear();
							/*mHandler.obtainMessage(Config.HANDLER_MSG_GETDATA_SUCCESS,
									"没有新的历史信息").sendToTarget();*/
							moremessageflag = false;
						}
						// 通知主线程数据发送成功
						mHandler.obtainMessage(
								Config.HANDLER_MSG_GETDATA_SUCCESS, searchhs)
								.sendToTarget();
					} else {
						mHandler.obtainMessage(
								Config.HANDLE_MSG_SEARCH_FAIL, message)
								.sendToTarget();// 发送获取消息失败
					}

				} catch (Exception e) {

					e.printStackTrace();
				}
			}else {
				String message = "请设置网络";
				mHandler.obtainMessage(Config.HANDLE_MSG_SEARCH_FAIL,
						message).sendToTarget();
			}
		}

	}

	/**
	 * 刷新下拉头中上次更新时间的文字描述。
	 */
	private String refreshUpdatedAtValue() {
		lastUpdateTime = preferences.getLong(UPDATED_AT + mId, -1);
		long currentTime = System.currentTimeMillis();
		long timePassed = currentTime - lastUpdateTime;
		long timeIntoFormat;
		String updateAtValue;
		if (lastUpdateTime == -1) {
			updateAtValue = getResources().getString(R.string.not_updated_yet);
		} else if (timePassed < 0) {
			updateAtValue = getResources().getString(R.string.time_error);
		} else if (timePassed < ONE_MINUTE) {
			updateAtValue = getResources().getString(R.string.updated_just_now);
		} else if (timePassed < ONE_HOUR) {
			timeIntoFormat = timePassed / ONE_MINUTE;
			String value = timeIntoFormat + "分钟";
			updateAtValue = String.format(
					getResources().getString(R.string.updated_at), value);
		} else if (timePassed < ONE_DAY) {
			timeIntoFormat = timePassed / ONE_HOUR;
			String value = timeIntoFormat + "小时";
			updateAtValue = String.format(
					getResources().getString(R.string.updated_at), value);
		} else if (timePassed < ONE_MONTH) {
			timeIntoFormat = timePassed / ONE_DAY;
			String value = timeIntoFormat + "天";
			updateAtValue = String.format(
					getResources().getString(R.string.updated_at), value);
		} else if (timePassed < ONE_YEAR) {
			timeIntoFormat = timePassed / ONE_MONTH;
			String value = timeIntoFormat + "个月";
			updateAtValue = String.format(
					getResources().getString(R.string.updated_at), value);
		} else {
			timeIntoFormat = timePassed / ONE_YEAR;
			String value = timeIntoFormat + "年";
			updateAtValue = String.format(
					getResources().getString(R.string.updated_at), value);
		}

		return updateAtValue;
	}

	/**
	 * 一分钟的毫秒值，用于判断上次的更新时间
	 */
	public static final long ONE_MINUTE = 60 * 1000;

	/**
	 * 一小时的毫秒值，用于判断上次的更新时间
	 */
	public static final long ONE_HOUR = 60 * ONE_MINUTE;

	/**
	 * 一天的毫秒值，用于判断上次的更新时间
	 */
	public static final long ONE_DAY = 24 * ONE_HOUR;

	/**
	 * 一月的毫秒值，用于判断上次的更新时间
	 */
	public static final long ONE_MONTH = 30 * ONE_DAY;

	/**
	 * 一年的毫秒值，用于判断上次的更新时间
	 */
	public static final long ONE_YEAR = 12 * ONE_MONTH;

	/**
	 * 上次更新时间的字符串常量，用于作为SharedPreferences的键值
	 */
	private static final String UPDATED_AT = "updated_at";

	/**
	 * 用于存储上次更新时间
	 */
	private SharedPreferences preferences;

	/**
	 * 上次更新时间的毫秒值
	 */
	private long lastUpdateTime;
	/**
	 * 为了防止不同界面的下拉刷新在上次更新时间上互相有冲突，使用id来做区分
	 */
	private int mId = -1;
}
